import { Component, OnInit } from '@angular/core'
import { Location } from '@angular/common'
import { ActivatedRoute, Router } from '@angular/router'
import { NzModalService, NzMessageService } from 'ng-zorro-antd'

import { PageService } from '@shared/services/page.service'
import { ETabScrollStatus } from '@shared/enums'
import { CommonAcquirerService } from '@shared/services/acquirer.service'
import { Acquirer, QrChannel, WXChannel, ALPChannel, UPIChannel, IChannelInfo } from '@shared/interfaces/acquirer'
import { DetailTabClass } from '@shared/classes/DetailTabClass'
import { getWXPChannelColumns, getALPChannelColumns, getUPIChannelColumns, getTRUChannelColumns } from './helper'
import { AcquirerService } from '../../acquirer.service'
import { I18NService } from '@core'
import { ECategoryUrl } from '@shared/interfaces/trade'
import { channelSorter } from 'app/utils/helpers'

@Component({
  selector: 'app-acquirer-detail-tab-channel-qr',
  templateUrl: './tab-channel-qr.component.html',
})
export class AcquirerDetailTabChannelQrComponent extends DetailTabClass<Acquirer> implements OnInit {
  acquirer: Acquirer

  channelInfos: IChannelInfo[] = []

  fields = {
    ChannelQR: '1',
  }

  constructor(
    public modelService: NzModalService,
    public msg: NzMessageService,
    public location: Location,
    public pageService: PageService,
    public router: Router,
    public route: ActivatedRoute,
    public acquirerService: AcquirerService,
    private commonAcquirerService: CommonAcquirerService,
    private i18n: I18NService,
  ) {
    super(pageService, route)
  }

  ngOnInit() {
    this.initialData()
  }

  initialData() {
    this.commonAcquirerService.viewAcquirer$.value$.subscribe(acquirer => {
      const { qrChannels } = acquirer
      this.acquirer = acquirer
      this.initColumns(qrChannels)
    })
    this.pageService.setTabScrollStatus(ETabScrollStatus.Scroll)
  }

  initColumns(qrChannels: QrChannel[]): void {
    // 每次都清空
    this.channelInfos = []
    qrChannels
      .sort((a: QrChannel, b: QrChannel) => channelSorter(a, b))
      .forEach((channel) => {
        switch (channel.chanCode) {
          case 'WXP':
            this.initWXPColumns(channel as WXChannel)
            break
          case 'ALP':
            this.initALPColumns(channel as ALPChannel)
            break
          case 'UPI':
            this.initUPIColumns(channel as UPIChannel)
            break
          case 'TRU':
            this.initTRUColumns(channel as UPIChannel)
        }
      })
  }

  initWXPColumns(channel: WXChannel): void {
    this.channelInfos.push({
      imgUrl: 'assets/images/centaur/logo/weixin-logo.png',
      channelName: this.trans('PAYMENT_BRAND.WXP'),
      channelColumns: getWXPChannelColumns(channel),
    })
  }

  initALPColumns(channel: ALPChannel): void {
    this.channelInfos.push({
      imgUrl: 'assets/images/centaur/logo/Alipay-logo.png',
      channelName: this.trans('PAYMENT_BRAND.ALP'),
      channelColumns: getALPChannelColumns(channel),
    })
  }

  initUPIColumns(channel: UPIChannel): void {
    this.channelInfos.push({
      imgUrl: 'assets/images/centaur/logo/upi-logo.png',
      channelName: this.trans('PAYMENT_BRAND.UPI'),
      channelColumns: getUPIChannelColumns(channel),
    })
  }

  initTRUColumns(channel: UPIChannel): void {
    this.channelInfos.push({
      imgUrl: 'assets/images/centaur/logo/TRU-logo.jpg',
      channelColumns: getTRUChannelColumns(channel),
    })
  }

  goDelete(index: number): void {
    this.modelService.confirm({
      nzTitle: this.trans('GENERAL.NOTIFICATION.WARNING'),
      nzContent: this.trans('BIZ.ACQUIRER.OTHER.QR_CHANNEL.DELETE_CHANNEL_CONFIRM'),
      nzOnOk: () => this.deleteChannel(index),
    })
  }

  deleteChannel(index: number): void {
    const insCode = this.acquirer.institution.common.insCode
    const channelId = this.acquirer.qrChannels[index].id
    this.acquirerService.deleteChannelForAcquirer(ECategoryUrl.qr, insCode, channelId).subscribe(isSuccess => {
      if (isSuccess) {
        this.msg.info(this.trans('GENERAL.NOTIFICATION.DELETE_SUCCESS'))
        this.acquirer.qrChannels = this.acquirer.qrChannels.filter(c => c.id !== channelId)
        this.commonAcquirerService.setViewingAcquirer(this.acquirer)
      }
    })
  }

  goAddChannel(): void {
    this.router.navigate(['../../qr/add'], { relativeTo: this.route })
  }

  // abstract methods
  onCancel() {}

  onSubmit() {}

  initialColumns() {}

  trans(key: string, params?: Object): string {
    return this.i18n.fanyi(key, params)
  }
}
